import * as ww from '@wecom/jssdk'
import { getConfigSignatureApi } from '@/service/index/login'

// 注册企业微信应用信息
export function registerWeChat() {
  return new Promise((resolve, reject) => {
    getConfigSignature()
    ww.register({
      corpId: 'ww861ba9424fc3d193', // 必填，当前用户企业所属企业ID
      agentId: 1000144, // 必填，当前应用的AgentID,不同环境的AgentID可能不同
      jsApiList: [
        'shareAppMessage',
        'hideMenuItems',
        'hideOptionMenu',
        'scanQRCode',
        'chooseImage',
        'getBluetoothAdapterState',
        'openEnterpriseChat',
        'sendChatMessage',
        'shareAppMessage',
        'shareWechatMessage',
        'shareToExternalContact',
        'getCurExternalContact',
        'startRecord',
        'stopRecord',
        'translateVoice',
      ], // 必填，需要使用的JSAPI列表
      getConfigSignature, // 必填，根据url生成企业签名的回调函数
      getAgentConfigSignature, // 必填，根据url生成企业签名的回调函数
      onConfigSuccess(res: any) {
        console.log('onConfigSuccess---res', res)
        resolve(res)
      },
      onConfigFail(res: any) {
        console.log('onConfigFail---res', res)
        reject(res)
      },
      onConfigComplete(res: any) {
        console.log('onConfigComplete---res', res)
      },
    })
  })

  async function getConfigSignature() {
    const { data } = await getConfigSignatureApi(1)
    console.log('getConfigSignatureApi', data)
    const { timestamp, nonceStr, signature } = data
    return { timestamp, nonceStr, signature }
  }
  async function getAgentConfigSignature() {
    const { data } = await getConfigSignatureApi(2)
    const nonceStr = data.noncestr
    const timestamp = data.timestamp
    const signature = data.signature
    return { timestamp, nonceStr, signature }
  }
}
